package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;




import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;


import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    public MemberService memberService;
    @Reference
    public OrderService orderService;
    @Reference
    public SetmealService setmealService;



    //会员的查询操作
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = memberService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return  pageResult;
    }


    //会员的添加操作
    @RequestMapping("/add")
    public Result add(@RequestBody Member member){
        try {
            memberService.add(member);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
    }


    //会员  根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Member member = memberService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //会员的编辑操作
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member){
        try {
            memberService.edit(member);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
    }


    //会员的删除操作
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            memberService.deleteById(id);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
    }


    //会员导出操作
    @RequestMapping("/exportReport")
    public Result exportReport(HttpServletRequest request, HttpServletResponse response){
        try {

            //获得Excel模板文件绝对路径
            String temlateRealPath = request.getSession().getServletContext().getRealPath("template") +
                    File.separator + "member.xlsx";
            //读取模板文件创建Excel表格对象
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));
            XSSFSheet sheet = workbook.getSheetAt(0);
            int rowId=3;
            //获得姓名性别手机号字段===机构信息伪造
            List<Member> list = memberService.findAll();
            for (Member member : list) {

                Integer id = member.getId();
                List<Order> orders = orderService.findByMemberId(id);
                for (Order order : orders) {
                    Integer setmealId = order.getSetmealId();
                    Setmeal setmeal = setmealService.findById(setmealId);
                    int cell=0;
                    XSSFRow row = sheet.createRow(rowId);
                    row.createCell(cell++).setCellValue(member.getName());
                    row.createCell(cell++).setCellValue(member.getSex());
                    row.createCell(cell++).setCellValue(member.getPhoneNumber());
                    row.createCell(cell++).setCellValue("机构地址");
                    row.createCell(cell++).setCellValue(setmeal.getName());

                    StringBuilder sb=new StringBuilder();
                    StringBuilder sb2 = new StringBuilder();
                    for (CheckGroup checkGroup : setmeal.getCheckGroups()) {
                        sb.append(checkGroup.getName());
                        for (CheckItem checkItem : checkGroup.getCheckItems()) {
                            sb2.append(checkItem.getName());
                        }
                    }
                    row.createCell(cell++).setCellValue(sb.toString());
                    row.createCell(cell).setCellValue(sb2.toString());
                }
                rowId++;
            }
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=member.xlsx");
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
            return new Result(true,"文件下载成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"文件下载失败");
        }
    }


}









